#include "camera.h"
#include "camera.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <QImage>
#include <QDebug>
#include <iostream>
camera::camera(QObject *parent) :
    QObject(parent)
{
    /* 实例化 */
    capture = new cv::VideoCapture();
    timer = new QTimer(this);

    connect(timer,SIGNAL(timeout()),this,SLOT(timerTimeOut()));
}

camera::~camera()
{
    delete capture;
    capture = NULL;
}

void camera::opencamera()
{
    qDebug() <<"open camera" <<endl;
    /* 如果有其他摄像头打开了，先释放 */
    if (capture->isOpened()) {
        capture->release();
    }
        capture->open(2);
}

void camera::closecamera()
{
    if(capture->isOpened()){
        capture->release();
        timer->stop();
    }

    else
        return;
}

void camera::GetLicensePlate()
{
    /* 如果有其他摄像头打开了，先释放 */
    if (capture->isOpened()) {
        capture->release();
    }

    /* 如果是ARM板子 打开/dev/video2 */
    if(QSysInfo::buildCpuArchitecture() == "arm")
    {
        capture->open(2);
    }
    else{
         /* 调试则使用Ubuntu 默认是video0 */
        capture->open(0);
    }



    static cv::Mat frame;
    *capture >> frame;
    if (frame.cols){
        /* 定义保存图片 */
        SaveImg = matToQImage(frame);
    }
}

bool camera::cameraProcess(bool bl)
{
    if (bl) {
        qDebug()<<"time go" <<endl;
        timer->start(33);
    } else {
        timer->stop();
    }
    /* 返回摄像头的状态 */
    return capture->isOpened();
}

QImage camera::matToQImage(const cv::Mat &img)
{
    /* USB摄像头和OV5640等都是RGB三通道，不考虑单/四通道摄像头 */
    if(img.type() == CV_8UC3) {
        /* 得到图像的的首地址 */
        const uchar *pimg = (const uchar*)img.data;

        /* 以img构造图片 */
        QImage qImage(pimg, img.cols, img.rows, img.step,
                      QImage::Format_RGB888);

        /* 在不改变实际图像数据的条件下，交换红蓝通道 */
        return qImage.rgbSwapped();
    }
    /* 返回QImage */
    return QImage();
}

void camera::SaveImage(QString typeflag,int Count)
{
    /* 判断图像是否为空 */
    if (!SaveImg.isNull()) {
        qDebug()<<"保存中。。。"<<endl;
        /* 定义保存路径 */
        QString filename;
        /* 如果是ARM板子  */
        if(QSysInfo::buildCpuArchitecture() == "arm"){
            if(typeflag == "car")
                filename = QString("/bishe/carId/car_%1.jpg").arg(Count);
            if(typeflag == "face")
                filename = QString("/bishe/faceId/face.jpg");
        }
        else{
            if(typeflag == "car")
                filename = QString("/home/lfl/Linux/bishe/QT/carID/car_%1.jpg").arg(Count);
            if(typeflag == "face")
                filename = QString("/home/lfl/Linux/bishe/QT/faceID/face.jpg");
        }


        /* 保存图片 */
        SaveImg.save(filename,"JPG",-1);

        qDebug()<<"保存完成！"<<endl;

        /* 发送图片信号 */
        emit readyImage(SaveImg);

        /* 关闭摄像头 */
        capture->release();
        //qDebug() << filename <<endl;
        if(typeflag == "car"){
            emit this->CarIDReady(filename);
        }

        if(typeflag == "face"){
            emit this->FaceReady(filename);
        }
    }
}

void camera::timerTimeOut()
{
    /* 如果摄像头没有打开，停止定时器，返回 */
    if (!capture->isOpened()) {
        timer->stop();
        return;
    }

    static cv::Mat frame;
    *capture >> frame;
    if (frame.cols)
        /* 发送图片信号 */
        emit readyImage(matToQImage(frame));
}
